from Actor import Actor;
from TopDownModule import TopDownModule;
import Image, ImageDraw, ImageFont;

class ActorMngr(object):
    def __init__(self, fileOut):
        self.fileOut = fileOut;
        self.nOldFrame = -1;
        self.aActors = dict();
        self.aActive_Actors = dict();
        self.topDown = TopDownModule(self);
        self.openDepthFiles();
        
    def openDepthFiles(self):
        self.fileIn_p23 = open("./Depth_Coords2_TD/person_23.txt", "r");
        self.fileIn_p24 = open("./Depth_Coords2_TD/person_24.txt", "r");
        self.fileIn_p25 = open("./Depth_Coords2_TD/person_25.txt", "r");
        self.fileIn_p26 = open("./Depth_Coords2_TD/person_26.txt", "r");
        self.fileIn_p27 = open("./Depth_Coords2_TD/person_27.txt", "r");
    
    def updateActors(self, aData): #Create & Update Actor's Data
        #Get Actor's Data from File
        nTrackId = aData[0];
        nMinX, nMinY = aData[1], aData[2];
        nMaxX, nMaxY, nFrame = aData[3], aData[4], aData[5]; #+1;
        sAction = aData[10] if len(aData)==11 else None;
        #If it's New Actor, then Create it
        if not self.aActors.has_key(nTrackId):
            self.aActors[nTrackId] = Actor(nTrackId, self);
        #Update Actor's Data
        self.update_PastFrame(nFrame); #Update only when Next Frames appears
        self.addActor_DepthCoords(nTrackId, nFrame, nMinX, nMinY, nMaxX, nMaxY, sAction); #Add the Depth Coordinates
#         self.aActors[nTrackId].updateActorData(nFrame, nMinX, nMinY, nMaxX, nMaxY, sAction);
        if not self.aActive_Actors.has_key(nTrackId):
            self.aActive_Actors[nTrackId] = self.aActors[nTrackId];
    
    def addActor_DepthCoords(self, nTrackId, nFrame, nMinX, nMinY, nMaxX, nMaxY, sAction):
        fileIn = None;
        actor = self.aActors[nTrackId];
        if nTrackId==23 and (nFrame>=1 and nFrame<=1890): fileIn = self.fileIn_p23;
        if nTrackId==24 and (nFrame>=890 and nFrame<=2058): fileIn = self.fileIn_p24;
        if nTrackId==25 and (nFrame>=1301 and nFrame<=1566): fileIn = self.fileIn_p25;
        if nTrackId==26 and (nFrame>=1333 and nFrame<=2195): fileIn = self.fileIn_p26;
        if nTrackId==27 and (nFrame>=982 and nFrame<=2011): fileIn = self.fileIn_p27;
        if fileIn==None: 
            actor.addDepthCoords(None, None); 
            return;
        aLine = fileIn.readline().replace("\n","").replace("\"","").split(" ");
        nX, nZ = float(aLine[0]), float(aLine[1]);
        nPerson_Score, nAction_Score = float(aLine[2]), float(aLine[3]);
        actor.addInit_Scores(nPerson_Score, nAction_Score);
        actor.addDepthCoords(nX, nZ);
        nX, nZ = nX*15+700, nZ*10+700;
        actor.updateActorData(nFrame, nX-1, nZ-1, nX+1, nZ+1, sAction);
        actor.minPosBB, actor.maxPosBB = [nMinX, nMinY], [nMaxX, nMaxY];
    
    def update_PastFrame(self, nFrame):
        #Update Actor's Activities & Write Current Frame (Only when Next Frame appears)
        self.nOldFrame = nFrame if self.nOldFrame==-1 else self.nOldFrame;
        if self.nOldFrame!=nFrame:
#             if (nFrame) % 1000==0: print "Frame:", nFrame;
#             for actor in self.aActive_Actors.values():
#                 actor.bMissing = actor.nFrame < self.nOldFrame;
            aDelActors = self.updateActivities(nFrame);
#             self.writeXML_PastFrame(); #Write Current Frame
#             self.write_PastFrame(); #Write Current Frame
#             self.draw_Results(nFrame);
            self.nOldFrame = nFrame;
            for nDelTrackId in aDelActors:
                del self.aActive_Actors[nDelTrackId];
#             self.printCurrentActors(nFrame-1);

    def updateActivities(self, nFrame):
        aDelActors = [];
        nBlankFrames = (nFrame - self.nOldFrame) if self.nOldFrame!=-1 else 1;
        nInitFrame = self.nOldFrame if self.nOldFrame!=-1 else nFrame;
        for actor in self.aActive_Actors.values():
            actor.clear_Bias();
        for i in range(nBlankFrames):
            nFrame = nInitFrame + i;
            for actor in self.aActive_Actors.values():
                actor.bMissing = actor.nFrame < nFrame;
                bNotAdded = not(actor.nTrackId in aDelActors);
                if actor.bMissing and bNotAdded and not(actor.check_Active()):
                    aDelActors.append(actor.nTrackId);
                #Block actors without DepthCoords
                if actor.nTrackId==23 and (nFrame<1): continue; #or nFrame>1890
                if actor.nTrackId==24 and (nFrame<890): continue; #or nFrame>2058
                if actor.nTrackId==25 and (nFrame<1301): continue;  #or nFrame>1566
                if actor.nTrackId==26 and (nFrame<1333): continue;  #or nFrame>2195
                if actor.nTrackId==27 and (nFrame<982): continue; #or nFrame>2011
                actor.updateActivities(nFrame); #Update Actor's Activities
                self.topDown.process_Actor(actor, nFrame); #Check the TopDown Process
#                 if actor.bMissing: continue;
#                 print nFrame, str(actor.getActorData()).translate(None,"[],'");
#         for actor in self.aActive_Actors.values():
#             print nFrame, actor.nTrackId, actor.nPerson_Bias;
        return aDelActors;
            
    def printCurrentActors(self, nFrame):
        aActorTracksId = [];
        for actor in self.aActive_Actors.values():
            aActorTracksId.append(actor.nTrackId);
        aActorTracksId.sort();
        print nFrame, aActorTracksId;
        
#     def writeXML_PastFrame(self): #Write Full Data to File
# #         print "Writting nFrame:", self.nOldFrame;
#         for actor in self.aActive_Actors.values():
#             if actor.bMissing: continue;
#             actData = str(actor.getActorData()).translate(None,"[],'") + "\n";
#             print self.nOldFrame, str(actor.getActorData()).translate(None,"[],'");
#             self.fileOut.write(actData);
        
    def write_PastFrame(self): #Write Full Data to File
#         print "Writting nFrame:", self.nOldFrame;
        for actor in self.aActive_Actors.values():
            if actor.bMissing: continue;
            actData = str(actor.getActorData()).translate(None,"[],'") + "\n";
            print self.nOldFrame, str(actor.getActorData()).translate(None,"[],'");
            self.fileOut.write(actData);
        
    def draw_Results(self, nFrame):
#         if nFrame<940: return;
#         if nFrame<1625: return;
#         if nFrame<1230: return;
#         if nFrame<1500: return;
#         if nFrame>=2059: return;
#         if nFrame<1650 or nFrame>2010: return;
#         return;
#         if not(nFrame in [1,2,3,4,5,6,7,8,9,10]): return;
#         if nFrame!=1050: return;
        print "Writing file ", nFrame;
#         sInputPath = "D:/PoL/Frames/";
#         sOutPath = "D:/PoL/Frames_Labeled/";
        sInputPath = "F:/iRobot_Movie/FinalFrames_22910-25106/Converted/";
        sOutPath = "F:/iRobot_Movie/FinalFrames_22910-25106/Acts_Labeled_Depth6_Graphs/";
#         sImgFile = "visualization " + str(nFrame).zfill(4) + ".jpg";        
        sImgFile = "iRobot Movie " + str(nFrame).zfill(6) + ".jpg";
        im = Image.open(sInputPath + sImgFile);
        draw = ImageDraw.Draw(im);
        font = ImageFont.truetype("timesbd.ttf", 14);
        self.draw_DepthCoords(draw, font);
        aTexts = [];
#         for actor in self.aActive_Actors.values():
#             print nFrame, actor.nTrackId, actor.nPerson_Bias;
#         self.testPIP(draw);
        for actor in self.aActive_Actors.values():
            if actor.bMissing: continue;
            #Person ID and Action
            nX1, nY1 = actor.minPosBB[0]+3, actor.minPosBB[1]-20;
            sTrackId = "p"+str(actor.nTrackId) +" "+ actor.sAction; #+" person "+actor.sAction;
            self.draw_Shadow_Text(nX1, nY1, sTrackId, draw, font);
            sPersonScore = "[%.2f + %.2f]" % (actor.nPerson_Score, actor.nPerson_Bias);
            sActionScore = "[%.2f + %.2f]" % (actor.nAction_Score, actor.nAction_Bias);
            sTextLine = "p"+str(actor.nTrackId)+sPersonScore +" "+ actor.sAction+sActionScore;
            aTexts.append(sTextLine);
            #Activity's Person & Confidence
#             self.draw_Activities(draw, font, actor);
            #Bounding Box
            self.draw_BBox(actor.minPosBB, actor.maxPosBB, "rgb(255, 0, 0)", draw);
            #Draw ROIs
#             for roi in actor.aROI_Persons:
#                 draw.ellipse([tuple(roi[0]), tuple(roi[1])], outline="blue");
        #Draw TextLines about Bias
        aTexts.sort();
        for i,sText in enumerate(aTexts):
            nX, nY = 10, 690;
            self.draw_Shadow_Text(nX, nY-i*20, sText, draw, font);
        #Draw Local Models Information
        self.draw_ActorTable(draw, font, 23, 10, 5);
        self.draw_ActorTable(draw, font, 24, 230, 5);
        self.draw_ActorTable(draw, font, 27, 10, 110);
        #Draw B3 Information
        self.draw_B3(im, nFrame);
        self.draw_B3_Plots(im, nFrame);
        del draw;
        im.save(sOutPath + sImgFile, "JPEG");
        
    def draw_DepthCoords(self, draw, font):
        nMinX, nMinY = 1030, 550;
        nMaxX, nMaxY = nMinX+240, nMinY+160;
        self.draw_BBox([nMinX,nMinY+30], [nMaxX-50,nMaxY], "rgb(0, 0, 0)", draw);
        draw.rectangle([(nMinX,nMinY+30), (nMaxX-50,nMaxY)], fill="rgb(170, 170, 170)");
        for actor in self.aActive_Actors.values():
            if actor.bMissing or actor.xP==None: continue;
            #Person ID in DepthCoords
            nX, nY = nMinX+actor.xP*2, nMinY+actor.zP*2;
#             sTrackId = "p"+str(actor.nTrackId);
#             self.draw_Shadow_Text(nX, nY, sTrackId, draw, font);
            #Actor's Position in DepthCoords
            if actor.nTrackId==23: draw.ellipse([(nX-2,nY-2), (nX+2,nY+2)], outline="blue", fill="blue");
            if actor.nTrackId==24: draw.ellipse([(nX-2,nY-2), (nX+2,nY+2)], outline="magenta", fill="magenta");
            if actor.nTrackId==25: draw.ellipse([(nX-2,nY-2), (nX+2,nY+2)], outline="red", fill="red");
            if actor.nTrackId==26: draw.ellipse([(nX-2,nY-2), (nX+2,nY+2)], outline="black", fill="black");
            if actor.nTrackId==27: draw.ellipse([(nX-2,nY-2), (nX+2,nY+2)], outline="green", fill="green");
        
    def draw_Activities(self, draw, font, actor):
        aTexts = [];
#         if actor.nTrackId==23:
#             if actor.activityMngr.approachAndMeet.has_key(24) and actor.activityMngr.approachAndMeet[24].bStarted and actor.activityMngr.approachAndMeet[24].nProb>0:
#                 aTexts.append("ApproachMeet(p23,p24)"+": %.2f" % (actor.activityMngr.approachAndMeet[24].nProb));
#             if actor.activityMngr.approachAndMeet.has_key(27) and actor.activityMngr.approachAndMeet[27].bStarted and actor.activityMngr.approachAndMeet[27].nProb>0:
#                 aTexts.append("ApproachMeet(p23,p27)"+": %.2f" % (actor.activityMngr.approachAndMeet[27].nProb));
#         elif actor.nTrackId==27:
#             if actor.activityMngr.approachAndMeet.has_key(23) and actor.activityMngr.approachAndMeet[23].bStarted and actor.activityMngr.approachAndMeet[23].nProb>0:
#                 aTexts.append("ApproachMeet(p27,p23)"+": %.2f" % (actor.activityMngr.approachAndMeet[23].nProb));
#         elif actor.nTrackId==24:
#             if actor.activityMngr.waitAndMeet1!=None and actor.activityMngr.waitAndMeet1.bStarted and actor.activityMngr.waitAndMeet1.nProb>0.05:
#                 aTexts.append("WaitMeet(p24,*)"+": %.2f" % (actor.activityMngr.waitAndMeet1.nProb));
#             if actor.activityMngr.waitAndMeet2.has_key(27) and actor.activityMngr.waitAndMeet2[27].bStarted and actor.activityMngr.waitAndMeet2[27].nProb>0.05:
#                 aTexts.append("WaitMeet(p24,p27)"+": %.2f" % (actor.activityMngr.waitAndMeet2[27].nProb));
#             if actor.activityMngr.walkTogether.has_key(27) and actor.activityMngr.walkTogether[27].bStarted and actor.activityMngr.walkTogether[27].nProb>0.05:
#                 aTexts.append("WalkTogether(p24,p27)"+": %.2f" % (actor.activityMngr.walkTogether[27].nProb));
        for nOtherTrackId in actor.activityMngr.approachAndMeet:
            if actor.activityMngr.approachAndMeet[nOtherTrackId].bStarted:
                aTexts.append("ApproachMeet(p%d,p%d): %.2f" % (actor.nTrackId, nOtherTrackId, actor.activityMngr.approachAndMeet[nOtherTrackId].nProb));
        for nOtherTrackId in actor.activityMngr.waitAndMeet2:
            if actor.activityMngr.waitAndMeet2[nOtherTrackId].bStarted:
                aTexts.append("WaitMeet(p%d,p%d): %.2f" % (actor.nTrackId, nOtherTrackId, actor.activityMngr.waitAndMeet2[nOtherTrackId].nProb));
        for nOtherTrackId in actor.activityMngr.walkTogether:
            if actor.activityMngr.walkTogether[nOtherTrackId].bStarted:
                aTexts.append("WalkTogether(p%d,p%d): %.2f" % (actor.nTrackId, nOtherTrackId, actor.activityMngr.walkTogether[nOtherTrackId].nProb));
        if actor.activityMngr.waitAndMeet1!=None and actor.activityMngr.waitAndMeet1.bStarted:
            aTexts.append("WaitMeet(p%d,*): %.2f" % (actor.nTrackId, actor.activityMngr.waitAndMeet1.nProb));
        for i,sText in enumerate(aTexts):
            self.draw_TextLine(i, sText, draw, font, actor);
    
    def draw_TextLine(self, nLine, sText, draw, font, actor):
        nX, nY = actor.minPosBB[0]+3, actor.maxPosBB[1]+(20*nLine);
        self.draw_Shadow_Text(nX, nY, sText, draw, font);
        
    def draw_Shadow_Text(self, x, y, text, draw, font):
        fillcolor = "white";
        shadowcolor = "black";
        # thin border
        draw.text((x-1, y), text, font=font, fill=shadowcolor);
        draw.text((x+1, y), text, font=font, fill=shadowcolor);
        draw.text((x, y-1), text, font=font, fill=shadowcolor);
        draw.text((x, y+1), text, font=font, fill=shadowcolor);        
        # thicker border
        draw.text((x-1, y-1), text, font=font, fill=shadowcolor);
        draw.text((x+1, y-1), text, font=font, fill=shadowcolor);
        draw.text((x-1, y+1), text, font=font, fill=shadowcolor);
        draw.text((x+1, y+1), text, font=font, fill=shadowcolor);
        # now draw the text over it
        draw.text((x, y), text, font=font, fill=fillcolor);
        
    def draw_BBox(self, minPosBB, maxPosBB, sColor, draw):
        x1, y1, x2, y2 = minPosBB[0], minPosBB[1], maxPosBB[0], maxPosBB[1];
        draw.rectangle([(x1,y1), (x2,y2)], outline=sColor);
        draw.rectangle([(x1,y1), (x2,y2)], outline=sColor);
        draw.rectangle([(x1-1,y1), (x2-1,y2)], outline=sColor);
        draw.rectangle([(x1,y1-1), (x2,y2-1)], outline=sColor);
        
    def draw_ActorTable(self, draw, font, nTrackId, nX, nY): 
        #Actor Information
        nLeft = 90;
        i,j = 0,0;
        kX1, kY1 = nX+(30*1)+nLeft-7, nY+20-2;
        kX2, kY2 = kX1+30, kY1+20;
        offsetX, offsetY = 30, 20;
#         draw.rectangle([(kX1+i*offsetX, kY1+j*offsetY), (kX2+i*offsetX, kY2+j*offsetY)], outline="blue");#, fill="blue");
        font2 = ImageFont.truetype("timesbd.ttf", 12);
#         draw.text((kX1+i*offsetX+5, kY1+j*offsetY+5), "0.87", font=font2, fill="white");
#         self.draw_Shadow_Text(kX1+i*offsetX+5, kY1+j*offsetY+5, "0.87", draw, font2);
#         i,j = 0,1;
#         draw.rectangle([(kX1+i*offsetX, kY1+j*offsetY), (kX2+i*offsetX, kY2+j*offsetY)], outline="black");#, fill="black");
#         i,j = 0,2;
#         draw.rectangle([(kX1+i*offsetX, kY1+j*offsetY), (kX2+i*offsetX, kY2+j*offsetY)], outline="black");#, fill="blue");
#         i,j = 0,3;
#         draw.rectangle([(kX1+i*offsetX, kY1+j*offsetY), (kX2+i*offsetX, kY2+j*offsetY)], outline="black");#, fill="red");
        #Draw Probabilities
        if not(self.aActors.has_key(nTrackId)) or self.aActors[nTrackId].bMissing: return;
        actor = self.aActors[nTrackId];
        person2i = {23:0, 24:1, 27:2};
        if actor.activityMngr.waitAndMeet1!=None and actor.activityMngr.waitAndMeet1.bStarted:
            i,j = person2i[actor.nTrackId], 0;
            sProb = "%.2f" % (actor.activityMngr.waitAndMeet1.nProb);
            nC = 150 - int(150*actor.activityMngr.waitAndMeet1.nProb)
            sColor = "rgb(%d,%d,255)" % (nC,nC);
            draw.rectangle([(kX1+i*offsetX, kY1+j*offsetY), (kX2+i*offsetX, kY2+j*offsetY)], outline=sColor, fill=sColor);
            self.draw_Shadow_Text(kX1+i*offsetX+5, kY1+j*offsetY+5, sProb, draw, font2);
        for nOtherTrackId in actor.activityMngr.waitAndMeet2:
            if actor.activityMngr.waitAndMeet2[nOtherTrackId].bStarted:
                i,j = person2i[nOtherTrackId], 1;
                sProb = "%.2f" % (actor.activityMngr.waitAndMeet2[nOtherTrackId].nProb);
                nC = 150 - int(150*actor.activityMngr.waitAndMeet2[nOtherTrackId].nProb)
                sColor = "rgb(%d,%d,255)" % (nC,nC);
                draw.rectangle([(kX1+i*offsetX, kY1+j*offsetY), (kX2+i*offsetX, kY2+j*offsetY)], outline=sColor, fill=sColor);
                self.draw_Shadow_Text(kX1+i*offsetX+5, kY1+j*offsetY+5, sProb, draw, font2);
        for nOtherTrackId in actor.activityMngr.approachAndMeet:
            if actor.activityMngr.approachAndMeet[nOtherTrackId].bStarted:
                i,j = person2i[nOtherTrackId], 2;
                sProb = "%.2f" % (actor.activityMngr.approachAndMeet[nOtherTrackId].nProb);
                nC = 150 - int(150*actor.activityMngr.approachAndMeet[nOtherTrackId].nProb)
                sColor = "rgb(%d,%d,255)" % (nC,nC);
                draw.rectangle([(kX1+i*offsetX, kY1+j*offsetY), (kX2+i*offsetX, kY2+j*offsetY)], outline=sColor, fill=sColor);
                self.draw_Shadow_Text(kX1+i*offsetX+5, kY1+j*offsetY+5, sProb, draw, font2);
        for nOtherTrackId in actor.activityMngr.walkTogether:
            if actor.activityMngr.walkTogether[nOtherTrackId].bStarted:
                i,j = person2i[nOtherTrackId], 3;
                sProb = "%.2f" % (actor.activityMngr.walkTogether[nOtherTrackId].nProb);
                nC = 150 - int(150*actor.activityMngr.walkTogether[nOtherTrackId].nProb)
                sColor = "rgb(%d,%d,255)" % (nC,nC);
                draw.rectangle([(kX1+i*offsetX, kY1+j*offsetY), (kX2+i*offsetX, kY2+j*offsetY)], outline=sColor, fill=sColor);
                self.draw_Shadow_Text(kX1+i*offsetX+5, kY1+j*offsetY+5, sProb, draw, font2);               
        #Persons
        self.draw_Shadow_Text(nX+(30*0)+nLeft, nY, "p%d"%(nTrackId), draw, font);
        self.draw_Shadow_Text(nX+(30*1)+nLeft, nY, "p23", draw, font);
        self.draw_Shadow_Text(nX+(30*2)+nLeft, nY, "p24", draw, font);
        self.draw_Shadow_Text(nX+(30*3)+nLeft, nY, "p27", draw, font);
        #Activities
        self.draw_Shadow_Text(nX+18, nY+(20*1), "Wait-N-Meet 1", draw, font);
        self.draw_Shadow_Text(nX+18, nY+(20*2), "Wait-N-Meet 2", draw, font);
        self.draw_Shadow_Text(nX, nY+(20*3), "Approach-N-Meet", draw, font);
        self.draw_Shadow_Text(nX+20, nY+(20*4), "WalkTogether", draw, font);
        #Table
        draw.rectangle([(nX-7,nY), (nX+(30*4)+nLeft-7,nY+(20*5)-2)], outline="black");
        draw.rectangle([(nX-7,nY), (nX+(30*4)+nLeft-7,nY+(20*1)-2)], outline="black");
        draw.rectangle([(nX-7,nY), (nX+(30*4)-7,nY+(20*5)-2)], outline="black");
        
    def draw_B3(self, im, nFrame):
#         nImg = nFrame;
        nImg=-1;
        if nFrame>=948 and nFrame<1012: nImg=1;
        if nFrame>=1012 and nFrame<1057: nImg=2;
        if nFrame>=1057 and nFrame<1080: nImg=3;
        if nFrame>=1080 and nFrame<1231: nImg=4;
        if nFrame>=1231 and nFrame<1325: nImg=5;
        if nFrame>=1325 and nFrame<1622: nImg=6;
        if nFrame>=1622 and nFrame<1651: nImg=7;
        if nFrame>=1651 and nFrame<1800: nImg=8;
        if nFrame>=1800 and nFrame<2010: nImg=9;
        if nFrame>=2010 and nFrame<2100: nImg=10;
        if nImg<0: return;
        sImgFile2 = "F:/iRobot_Movie/FinalFrames_22910-25106/B3_Graph/B3_Graph_%d.png" % (nImg);
        im2 = Image.open(sImgFile2);
        im2 = im2.resize((int(im2.size[0]*0.5),int(im2.size[1]*0.5)), Image.ANTIALIAS);
        im.paste(im2, (450,5), im2);
        
    def draw_B3_Plots(self, im, nFrame):
#         nImg = nFrame;
        nImg=-1;
        if nFrame>=948 and nFrame<1012: nImg=1;
        if nFrame>=1012 and nFrame<1057: nImg=2;
        if nFrame>=1057 and nFrame<1080: nImg=3;
        if nFrame>=1080 and nFrame<1231: nImg=4;
        if nFrame>=1231 and nFrame<1325: nImg=5;
        if nFrame>=1325 and nFrame<1622: nImg=6;
        if nFrame>=1622 and nFrame<1651: nImg=7;
        if nFrame>=1651 and nFrame<1800: nImg=8;
        if nFrame>=1800 and nFrame<2010: nImg=9;
        if nFrame>=2010 and nFrame<2100: nImg=10;
        if nImg<0: return;
        #p23
        sImgFile_p23 = "F:/iRobot_Movie/FinalFrames_22910-25106/B3_Posteriors/p23_%d.png" % (nImg);
        im_p23 = Image.open(sImgFile_p23);
        im_p23 = im_p23.resize((int(im_p23.size[0]*0.75),int(im_p23.size[1]*0.75)), Image.ANTIALIAS);
        im.paste(im_p23, (20,500));
        #p24
        sImgFile_p24 = "F:/iRobot_Movie/FinalFrames_22910-25106/B3_Posteriors/p24_%d.png" % (nImg);
        im_p24 = Image.open(sImgFile_p24);
        im_p24 = im_p24.resize((int(im_p24.size[0]*0.75),int(im_p24.size[1]*0.75)), Image.ANTIALIAS);
        im.paste(im_p24, (430,500));
        #p27
        sImgFile_p27 = "F:/iRobot_Movie/FinalFrames_22910-25106/B3_Posteriors/p27_%d.png" % (nImg);
        im_p27 = Image.open(sImgFile_p27);
        im_p27 = im_p27.resize((int(im_p27.size[0]*0.75),int(im_p27.size[1]*0.75)), Image.ANTIALIAS);
        im.paste(im_p27, (840,500));
        